﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace BPC.Magic8Ball.WebParts
{
    public class Ball : System.Web.UI.WebControls.WebParts.WebPart 
    {

        Label _answerLabel = new Label();
        Panel _answerPanel = new Panel();
        TextBox _question = new TextBox();
        Button _ask = new Button();

        string _answerList = "StandardAnswers";

        [Personalizable(PersonalizationScope.Shared),
        WebBrowsable(true),
        System.ComponentModel.Category("Magic 8 Ball"),
        WebDisplayName("List name"),
        WebDescription("Name of a list on the current site")]
        public string Message
        {
            get { return _answerList; }
            set { _answerList = value; }
        }

        protected override void CreateChildControls()
        {

            _answerPanel.Width = new Unit(300);
            _answerPanel.Height = new Unit(300);
            _answerPanel.Style.Add("z-index", "3");

            _answerLabel.Text = "Ask the Eight Ball";
            _answerLabel.Width = new Unit(100);
            _answerLabel.Height = new Unit(100);
            _answerLabel.Style.Add("Top", "80");
            _answerLabel.Style.Add("Left", "100");
            _answerLabel.Style.Add("position", "relative");
            _answerLabel.Style.Add("text-align", "center");
            _answerLabel.Font.Size = new FontUnit(15);

            _answerPanel.Controls.Add(_answerLabel);
            _answerPanel.BackImageUrl = "_layouts/images/magic8ball/8-ball.jpg";

            this.Controls.Add(_answerPanel);

            Panel questionPanel = new Panel();
            questionPanel.Width = new Unit(300);
            questionPanel.Style.Add("text-align", "center");
            questionPanel.Style.Add("Top", "40");

            _ask.Text = "Ask?";
            _ask.Click += new EventHandler(_ask_Click);
            questionPanel.Controls.Add(_question);
            questionPanel.Controls.Add(_ask);
            this.Controls.Add(questionPanel);

            base.CreateChildControls();
        }

        void _ask_Click(object sender, EventArgs e)
        {
            Magic8Ball.Ball ball = new Magic8Ball.Ball();
            Magic8Ball.SharePoint sharePoint = new Magic8Ball.SharePoint();

            ball.Answers = sharePoint.GetAnswersFromList(_answerList);
            _answerLabel.Text = ball.AskQuestion(_question.Text);
        }
    }
}
